package cuongnh.project.japanesediary.vocabulary.dao;

import java.util.Date;
import java.util.List;
import java.util.Set;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import cuongnh.project.japanesediary.vocabulary.model.Grammar;

@Repository
@Transactional
public class GrammarDao extends HibernateDaoSupport {
	
	public void save(Grammar a1) {
		a1.setCreatedDate(new Date());
		a1.setModifiedDate(new Date());
		getHibernateTemplate().save(a1);
	}

	/**
	 * 'load' returns a proxy with only the id populated. To access members in
	 * the proxy requires a Hibernate session. Haven't figured out how to do
	 * transactions between bundles yet...
	 */
	public Grammar load(long id) {
		return (Grammar) getHibernateTemplate().load(Grammar.class, id);
	}

	public Grammar get(long id) {
		return (Grammar) getHibernateTemplate().get(Grammar.class, id);
	}

	public void update(Grammar a1) {
		a1.setModifiedDate(new Date());
		getHibernateTemplate().update(a1);
	}

	public void delete(Grammar a1) {
		getHibernateTemplate().delete(a1);
	}

	public Grammar merge(Grammar a1) {
		a1.setModifiedDate(new Date());
		return (Grammar) getHibernateTemplate().merge(a1);
	}
	
	public void saveOrUpdate(Grammar a1) {
		a1.setModifiedDate(new Date());
		if(a1.getCreatedDate() == null){
			a1.setCreatedDate(new Date());
		}
		getHibernateTemplate().saveOrUpdate(a1);
	}

	public List<Grammar> getAll() {
		return getHibernateTemplate().loadAll(Grammar.class);
	}
	
	public Grammar getGrammarWithExamples(long grammarId) {
		Grammar grammar = getHibernateTemplate().get(Grammar.class,				
				grammarId);
		getHibernateTemplate().initialize(grammar.getGrammarExamples());
		
		return grammar;
	}
	
	public Grammar getGrammarWithRelateds(long grammarId) {
		Grammar grammar = getHibernateTemplate().get(Grammar.class,
				grammarId);
		getHibernateTemplate().initialize(grammar.getRelatedsGrammar());
		return grammar;
	}
	
	public Grammar getGrammarWithCategories(long grammarId) {
		Grammar grammar = getHibernateTemplate().get(Grammar.class,
				grammarId);
		getHibernateTemplate().initialize(grammar.getGrammarCategories());
		return grammar;
	}
	
	public Set<Grammar> getSynonymGrammars(long grammarId) {
		Grammar grammar = getHibernateTemplate().get(Grammar.class,
				grammarId);
		getHibernateTemplate().initialize(grammar.getGrammarSynonyms());
		return grammar.getGrammarSynonyms();
	}
	
	public Grammar getGrammarWithSynonyms(long grammarId) {
		Grammar grammar = getHibernateTemplate().get(Grammar.class,
				grammarId);
		getHibernateTemplate().initialize(grammar.getGrammarSynonyms());
		return grammar;
	}
	
	public Set<Grammar> getAntonymGrammars(long grammarId) {
		Grammar grammar = getHibernateTemplate().get(Grammar.class,
				grammarId);
		getHibernateTemplate().initialize(grammar.getGrammarAntonyms());
		return grammar.getGrammarAntonyms();
	}
	
	public Grammar getGrammarWithAntonyms(long grammarId) {
		Grammar grammar = getHibernateTemplate().get(Grammar.class,
				grammarId);
		getHibernateTemplate().initialize(grammar.getGrammarAntonyms());
		return grammar;
	}
	
	public Grammar getGrammarWithVocabularies(long grammarId) {
		Grammar grammar = getHibernateTemplate().get(Grammar.class,
				grammarId);
		getHibernateTemplate().initialize(grammar.getVocabularies());
		return grammar;
	}

}
